package pos.MainMenu;

import javax.media.j3d.*;
import javax.vecmath.*;
import pos.utils.*;
/**
 * 主菜单界面的背景元素
 * @author plus
 */
public class MainMenuSurrendings extends BranchGroup implements Invokeable{
    private static Group StaticObjects=null;
    private static Group [] MovingObjects= new Group[2];
    private static TransformGroup [] TGMovingObjects=new TransformGroup[2];
    private static Transform3D [] T3DMovingObjects=new Transform3D[]{new Transform3D(),new Transform3D()};
    private double [] posX = new double [2];
    /**
     * 构造函数，载入对象
     */
    public MainMenuSurrendings() {
        this.setCapability(ALLOW_DETACH);
        if(StaticObjects==null || MovingObjects==null)
            initObjects();
        this.addChild(StaticObjects);
        this.addChild(TGMovingObjects[0]);
        this.addChild(TGMovingObjects[1]);
        this.compile();
    }

    /**
     * 为条件调用器调用的方法
     * @param Step 条件调用器送回的步进值
     */
    public void invoke(double Step) {
        setBackPosition(Step*30);
    }
    
    private void setBackPosition(double Step){
        for (int posID=0; posID<2; posID++){
            posX[posID]-=Step;
            if (posX[posID]<-30) posX[posID]+=80;
            T3DMovingObjects[posID].set(new Vector3d(posX[posID],0,0));
            TGMovingObjects[posID].setTransform(T3DMovingObjects[posID]);
        }
    }
    
    private void initObjects(){
        StaticObjects=ObjectFactory.createObject("Model.MainMenu.Static");
        MovingObjects[0]=ObjectFactory.createObject("Model.MainMenu.Back");
        MovingObjects[1]=(Group)MovingObjects[0].cloneTree();
        TGMovingObjects[0]=new TransformGroup();
        TGMovingObjects[1]=new TransformGroup();
        TGMovingObjects[0].setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        TGMovingObjects[1].setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        TGMovingObjects[0].addChild(MovingObjects[0]);
        TGMovingObjects[1].addChild(MovingObjects[1]);
        posX[0]=20;
        posX[1]=70;
        setBackPosition(0);
    }
}
